package net.cathackers.devel.scmp.auth.impl.jabber;

import net.cathackers.devel.scmp.auth.interfaces.UserID;

public class JabberUserID implements UserID {
    private String userID_;
    private String resource_;
    
    public JabberUserID(String userID, String resource) {
	resource_ = resource;
	userID_ = userID;
    }
    public JabberUserID(String fullCanonical) {
	String[] parts = fullCanonical.split("/");
	userID_ = parts[0];
	if (parts.length > 1) {
	    resource_ = parts[1];
	}
    }
    @Override
    public String getCanonicalName() {
	String ret;
	if (resource_ == null) {
	  ret = userID_;  
	} else {
	  ret = userID_ + "/" + resource_;  
	}
	return ret;
    }

    @Override
    public String getDomain() {
	return userID_.substring(userID_.indexOf('@') + 1);
    }

    @Override
    public String getID() {
	return userID_;
    }

    @Override
    public String getResource() {
	return resource_;
    }

    @Override
    public String getUsername() {
	return userID_.substring(0, userID_.indexOf('@'));
    }
    @Override
    public boolean equals(Object obj) {
	// UserID1 == UserID2 if the ID components are equal
	// Even if they have different Resources.
	// Because in practice, they are the same user.
        UserID target = (UserID) obj;
        if (userID_.equalsIgnoreCase(target.getID())) {
            return true;
        } else {
            return false;
        }
    }
    @Override
    public int hashCode() {
        return userID_.hashCode();
    }
    
    public UserID getGeneralUserID(){
	return new JabberUserID(userID_);
    }
}
